/*
    Radio Battletoads Player
    Copyright (C) 2011 Jorge Suarez de Lis <yo@jorgesuarezdelis.name>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.radiobattletoads.player;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.TimerTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class UpdateSchedule extends TimerTask {
	
	static String[] schedule = null;
	static public Boolean firstrun = true;

	public void run(){

		Message m = new Message();
		
		if(RadioBattletoadsActivity.instantiatedActivity!=null){
			m.what = RadioBattletoadsActivity.GOING_TO_UPDATE_SCHEDULE;
			RadioBattletoadsActivity.instantiatedActivity.h.sendMessage(m);
		}
		
		if( (UpdateSchedule.firstrun==false && UpdateSchedule.schedule!=null) || UpdateSchedule.schedule==null){
			schedule = this.downloadSchedule();
			UpdateSchedule.firstrun = false;
		}
		
		if(RadioBattletoadsActivity.instantiatedActivity!=null){
			update(schedule);
		}
		
		UpdateNowPlaying.firstrun=false;
		
	}
	
	public static void update(String[] schedule){
		Message m2 = new Message();
		if(schedule!=null){
			m2.what = RadioBattletoadsActivity.UPDATE_SCHEDULE;
			m2.getData().putStringArray("schedule", schedule);
			RadioBattletoadsActivity.instantiatedActivity.h.sendMessage(m2);
		}
		else{
			m2.what = RadioBattletoadsActivity.UPDATE_SCHEDULE_FAILED;
			RadioBattletoadsActivity.instantiatedActivity.h.sendMessage(m2);
		}
	}
	
	private String[] downloadSchedule(){
    	HttpClient hc = new DefaultHttpClient();
    	HttpGet hg = new HttpGet("http://www.radiobattletoads.com/schedule.php");
    	String[] now = new String[20];
    	UpdateSchedule.schedule = null;
    	try {
			HttpResponse hr = hc.execute(hg);
			InputStream is = hr.getEntity().getContent();
			
			final char[] buffer = new char[0x10000];
			StringBuilder out = new StringBuilder();
			Reader in = new InputStreamReader(is, "UTF-8");
			int read;
			do {
			  read = in.read(buffer, 0, buffer.length);
			  if (read>0) {
			    out.append(buffer, 0, read);
			  }
			} while (read>=0);
			
			String xmlcontent = out.toString();
			Log.d("schedule","Refreshing schedule");
			
			int i,f;
			int start = 0;
			int q=0;
			while(xmlcontent.indexOf("<showTime>", start)!=-1){
				i = xmlcontent.indexOf("<showTime>",start);
				f = xmlcontent.indexOf("</showTime>",i);
				String showTime = xmlcontent.substring(i+10, f);
				i = xmlcontent.indexOf("<showTitle>",start);
				f = xmlcontent.indexOf("</showTitle>",i);
				String showTitle = xmlcontent.substring(i+11, f);
				Log.d("schedule","Adding " + showTitle);
				now[q] = showTime + "#" + showTitle;
				start=f;
				q++;
    	    }
			now[q]=null;
			if(q>0) return now;
			else return null;
			
		} catch (IOException e) {
			return null;
		}
    }
}
